/* $Header: sigpath.h,v 2.2 01/10/02 10:30:41 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  sigpath.h                                       */
/* ==================                                                  */
/*                                                                     */
/* Author:   Lesley Freed                                              */
/*                                                                     */
/* COMMENT: This file contains all of the function prototypes for      */
/*          signal path for IQ.                                        */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef SGPTH_H
#define SGPTH_H


/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/**********************************/
/* End of Signal Type Definitions */
/**********************************/

/* TCD Polarity */

#define TCD_POS_POLARITY  0x00  /* TCD polarity = Positive                    */
#define TCD_NEG_POLARITY  0x01  /* TCD polarity = Negative                    */

/* Signal Limits */

#define START_SIGNAL_MARKER     0x80000000 /* Invalid 32-bit signed value     */
                                           /* indicating START run occurred   */
#define STOP_SIGNAL_MARKER      0x80000001 /* Invalid 32-bit signed value     */
                                           /* indicating STOP run occurred    */
#define START_STOP_SIGNAL_MARKER 0x80000002 /* Invalid 32-bit signed value    */
                                            /* indicating run occurred        */
                                            /* without any data points.       */
#define MAX_SIGNED_32BIT_VALUE  0x7fffffff
#define MIN_SIGNED_32BIT_VALUE  0x80000003
#define MAX_24BIT_VALUE         0x00ffffff
#define MAX_26BIT_VALUE         0x03ffffff

#define START_FLAG_SIGNAL       0x80000000/* Flag inserted in signal stream  */
                                           /* to indicate a start occurred    */
#define STOP_FLAG_SIGNAL        0x80000001/* Flag inserted in signal stream  */
                                           /* to indicate a stop occurred     */
#define EXTERN_FLAG_SIGNAL      0x80000002 /* Flag inserted in signal stream  */
                                           /* to indicate an external event   */

/* Signal Rate */

#define DR_200HZ        0x00    /* 0 = 200 Hz                                 */
#define DR_100HZ        0x01    /* 1 = 100 Hz                                 */
#define DR_50HZ         0x02    /* 2 = 50 Hz                                  */
#define DR_20HZ         0x03    /* 3 = 20 Hz                                  */
#define DR_20HZ_RAW     0x04    /* 4 = 20 Hz unfiltered data for bunching     */

/* Start and Stop Run Constants */

#define TIME_INTERVAL_100HZ 160 /* # of 62.5 usec ticks in a 100 Hz interval  */

/* DAC and ATTN Constants */

#define ATTN_TICK_SIZE  8         /* Size of tick in 8-bit attenuator output  */

#define ATTN_START_TICK_LENGTH 40 /* .2 sec start tick mark ( This is for     */
                                  /* 2 signals at 100 Hz to decrement for:    */
                                  /* 1/100 Hz * 40/2 = .2 seconds )           */
#define DAC_OFFSET      0x13106   /* 2.383 counts in the upper 8-bits --> 0 V */
                                  /* divided by 32/16, the gain of dac filters*/

#define ATTN_OFFSET 0xa00          /* 4% of 16-bit value 0xffff               */

#define ATTN_OFFSET_SCALED 0x120   /* 4% of 16-bit value 0xffff divided by 8* */
                                   /* 1.11 = 8.88, the gain of the attn filter*/
#define ATTN_OFFSET_OUTPUT 0xa     /* 4% of 8-bit value 0xff                  */

#define DAC_RATE_3HZ    0          /* 3 Hz DAC_rate value                     */
#define DAC_RATE_30HZ   1          /* 30 Hz DAC_rate value                    */

/*****************************/
/*   EXTERNALS               */
/*****************************/

extern const PLOT_CONSTANTS MON_5V_CONSTS;
extern const PLOT_CONSTANTS MON_15VPOS_CONSTS;
extern const PLOT_CONSTANTS MON_15VNEG_CONSTS;
extern const U_INT32 UECD_TBL[];
extern const U_INT32 UECD_TBL_ATN[];
extern const U_INT32 LDID_TBL[];
extern const U_INT32 LDID_TBL_ATN[];

/*****************************/
/*   MACROS                  */
/*****************************/

#define START_RUN_FOR_SCC_EXPAND()                                             \
{                                                                              \
                                                                               \
   /* Check to see if we have had a chance to stop the last scc profile */     \
   if ( Scc_Run_State == NOT_IN_RUN )                                          \
   {                                                                           \
      Scc_Run_State = RUN_IN_PROGRESS;                                         \
   }                                                                           \
   else                                                                        \
   {                                                                           \
      /* Flag that we need to start a new profile */                           \
      Scc_Start_Pending = TRUE;                                                \
   }                                                                           \
                                                                               \
}                                                                              \


#define EMIT_ATTN_START_TICK()                                                 \
{                                                                              \
                                                                               \
   /* Set a counter so that the attenuator emits a start tick mark */          \
   Attn_Start_Tick_Count = ATTN_START_TICK_LENGTH;                             \
                                                                               \
}                                                                              \


/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

#pragma SECTION PROG=SysCode

        /* sigpath.c functions */

        void    SigPath(void);          /* fcn to process signals             */
        void    HandleStartSignal(void);
        void    ProcessStartSignal(void);
        void    HandlePendingStartSignal(void);
        void    HandleStopSignal(void);
        void    ProcessStopSignal(void);
        U_INT32 LinUECD( const U_INT32 tbl[], INT32 x );
        U_INT32 LinLDID( const U_INT32 tbl[], INT32 x );

        /* sig_analog.c functions */

        void    DACSignalPath( INT32 s_signal ); /* DAC Output Processing     */
        void    ATTNSignalPath( INT32 s_signal ); /* ATTN Output Processing   */

        /* scc.c functions */

        INT32   ApplySCC_32( INT32 );              /* fcn to apply SCC for    */
                                                   /*  analog signal path     */
        void    ApplySCC( pDIG_DATA psig );        /* fcn to apply SCC for    */
                                                   /*  digital signal path    */

        void    ExpandSCC( pCCOMP_TABLE pccomp);   /* fcn to calculate a sig- */
                                                   /* nal value.              */

        INT32   CalcSccSlope ( pCCOMP_TABLE pccomp, U_INT16 N );
        INT32   CalcScc2ndDeriv( pCCOMP_TABLE pccomp, U_INT16 N, U_INT16 M );

        void    CollectSCC( pCCOMP_TABLE pccomp);  /* fcn to collect an SCC   */
                                                   /* profile.                */
        void    ServiceCompressedSignals( void );  /* fcn to handle SCC and   */
                                                   /* test plot.              */

        /* scc_init.c functions */
        void     ResetScc( void );

        /* sig_math.c functions */

        LDIV_TYPE Div10( U_INT32 data, U_INT32 remainder);
        LDIV_TYPE Scale30Hz( U_INT32 multiplicand, U_INT32 remainder);
        LDIV_TYPE Scale3Hz( U_INT32 multiplicand, U_INT32 remainder);
        LDIV_TYPE ScaleNoFilter( U_INT32 multiplicand, U_INT32 remainder );
        LDIV_TYPE ScaleAttn3Hz( U_INT32 multiplicand, U_INT32 remainder);
        BUNCH_SUM FiltSum( INT32 signal, INT32 old_signal,
                           INT32 sum_hi, INT32 sum_lo);
        BUNCH_SUM BunchSum32( INT32 signal, INT32 sum_hi, INT32 sum_lo);
        BUNCH_SUM BunchSum(INT16 sig_hi,INT32 sig_lo,INT32 sum_hi,INT32 sum_lo);
        void      BunchOut( INT32 divisor, INT32 offset, pBUNCH_SUM psum,
                            pBUNCH_OUT pout );
        BUNCH_OUT32 BunchOut32( INT32 sum_hi, INT32 sum_lo, U_INT32 divisor,
                                INT32 remainder);

        void      BoxCarSum3( pBUNCH_SUM psum, pBUNCH_SUM pfilt_sum,
                              pBUNCH_SUM psum_table );
        BUNCH_SUM ResetBoxCarSum3( pBUNCH_SUM psum );
        U_INT32   TblMult( U_INT32 mult1, U_INT32 mult2 );

#pragma SECTION UNDO

        /* sig_init.c functions */

        void     InitializeSigPath(void);
        void     InitializePwrFailSigPath(void);
        void     InitializeDspOut(void);
        void     SetSigPathDefaultSetpts(void);
        void     UpdateSignalStatus(pSIG_TABLE psignal);
        void     UpdateSigPathStatus(void);
        void     SetSigPathDefaultSetpts( void );
        SIG_TYPE GetDefaultSignal( U_INT8 chan );
        void     ResolveSigPathPowerOnSetpts( void );

        /* sig_util.c functions */

        INT32 ConvertToMv( INT32 value, pPLOT_CONSTANTS pconstants );
        void  ConvertPlotValueToSignalUnits( void );
        INT32 GetSignalFromSizeAndAddr( DATA_SIZE size, U_INT32 *addr );
#pragma SECTION PROG=SysCode
        void FilterSignal32( pSIG_FILTER pfilt, INT32 s_signal, U_INT16 size );
        void SetSigpathStopMarkers( void );
#pragma SECTION UNDO

        /* sig_cmd.c functions */

        void SetAltSignalType(U_INT8 chan, SIG_TYPE type, U_INT32 *addr,
                              U_INT16 msg_no);
        void SetSigType(U_INT8 chan, SIG_TYPE type);
        void CycleThruDataRates( void );

        /* scc_init.c functions */

        UI_ERR  StartBlankRun( CCOMP ccomp, INT32 local );
        void    StopBlankRun( void );
        void    InitializeSccRun( void );
        void    InitializeProfile( pCCOMP_TABLE pccomp);
        void    InitializeScc( void );
        void    StopRunForScc( void );
        void    StopScc( pCCOMP_TABLE pccomp );

        /* pneu_sig.c functions */

        void    SetPlotConstants( SIG_TYPE type, PLOT_CONSTANTS *values );
        void    SigPresUnitsChanged( void );
        void    SigDetDiagLinearModeChanged( void );

        /* sig_diag.c functions */

        void    SetSignalDspFault( BIT8 dsp_status );
#endif
